package com.kx.smartdevice.controller;

import com.github.pagehelper.PageInfo;
import com.kx.smartdevice.dto.R;
import com.kx.smartdevice.pojo.Area;
import com.kx.smartdevice.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName AreaController
 * @Author Sun
 * @Date 2021/5/29 16:14
 */
@RestController
@RequestMapping("/areas")
public class AreaController {

    @Autowired
    private AreaService areaService;

    @GetMapping("/areas")
    public R getAllAreas(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit,@RequestParam(defaultValue = "") String areaname, @RequestParam(defaultValue = "-100") int status){
        PageInfo<Area> pageInfo= areaService.findAllAreaByPage(page,limit,areaname,status);
        return R.setOK(pageInfo);
    }


    @GetMapping("/area/{id}")
    public R getAreaById(@PathVariable Long id) {

        Area area = areaService.getAreaById(id);
        return R.setOK(area);
    }

    @GetMapping("/area/parent/{id}")
    public R getAreaByParentId(@PathVariable Long id) {
        List<Area> areaList = areaService.getAreaByParentId(id);
        return R.setOK(areaList);
    }

    @PutMapping("/area")
    public R addAreas(@RequestBody Area area){
        areaService.addAreas(area);
        return R.setOK(null);
    }

    @PostMapping("/area")
    public R updateArea(@RequestBody Area area){
        int result = areaService.updateArea(area);
        return R.setOK(result);
    }

    @DeleteMapping("/areas")
    public R deleteAreaByIds(String[] ids){
        int result = areaService.deleteAreaByIds(ids);
        return R.setOK(result);
    }

    /**
     * 查询一级区域
     * @param status
     * @return
     */
    @GetMapping("/areas/parent")
    public R getAllFirstParentAreas(@RequestParam(defaultValue = "-100") int status) {
        List<Area> areaList = areaService.getAllFirstParentAreas(status);
        return R.setOK(areaList);
    }
}
